from flask import request
from app.utils.response import response_success, response_created, response_not_found, response_fail, response_updated, response_deleted
from app.api.services.user_service import UserService


class UserController:
    """用户控制器类，处理HTTP请求和响应"""
    
    @staticmethod
    def get_all_users():
        """获取所有用户"""
        try:
            users = UserService.get_all_users()
            return response_success(data=users)
        except Exception as e:
            return response_fail(msg=f"获取用户列表失败: {str(e)}")
    
    @staticmethod
    def get_user_by_id(user_id):
        """根据ID获取用户"""
        try:
            user = UserService.get_user_by_id(user_id)
            if user:
                return response_success(data=user)
            else:
                return response_not_found(msg=f"用户ID {user_id} 不存在")
        except Exception as e:
            return response_fail(msg=f"获取用户失败: {str(e)}")
    
    @staticmethod
    def get_user_by_params():
        """通过查询参数获取用户"""
        try:
            user_id = request.args.get('user_id', type=int)
            if not user_id:
                return response_fail(msg="缺少user_id参数")
                
            user = UserService.get_user_by_id(user_id)
            if user:
                return response_success(data=user)
            else:
                return response_not_found(msg=f"用户ID {user_id} 不存在")
        except Exception as e:
            return response_fail(msg=f"获取用户失败: {str(e)}")
    
    @staticmethod
    def create_user():
        """创建新用户"""
        try:
            data = request.get_json()
            
            # 验证请求数据
            if not data or not data.get('username') or not data.get('email') or not data.get('password'):
                return response_fail(msg="用户名、邮箱和密码不能为空")
            
            # 创建用户
            user, error = UserService.create_user(
                username=data['username'],
                email=data['email'],
                password=data['password']
            )
            
            if user:
                return response_created(data=user, msg="用户创建成功")
            else:
                return response_fail(msg=error)
        except Exception as e:
            return response_fail(msg=f"创建用户失败: {str(e)}")
    
    @staticmethod
    def update_user(user_id):
        """更新用户信息"""
        try:
            data = request.get_json()
            
            # 更新用户
            user, error = UserService.update_user(
                user_id=user_id,
                username=data.get('username'),
                email=data.get('email'),
                password=data.get('password'),
                is_active=data.get('is_active')
            )
            
            if user:
                return response_updated(data=user, msg="用户更新成功")
            else:
                return response_fail(msg=error)
        except Exception as e:
            return response_fail(msg=f"更新用户失败: {str(e)}")
    
    @staticmethod
    def delete_user(user_id):
        """删除用户"""
        try:
            success, error = UserService.delete_user(user_id)
            
            if success:
                return response_deleted(msg="用户删除成功")
            else:
                return response_fail(msg=error)
        except Exception as e:
            return response_fail(msg=f"删除用户失败: {str(e)}")